package me.wangshier.leetcode.easy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 5. 寻找最大葫芦
 * @Author: tongqianwen
 * @Date: 2025/3/2
 */
public class FindMaxGourd {

    public static int[] solution(int n, int max, int[] array) {
        // 统计每种牌面值的出现次数
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int num : array) {
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
        }

        // 存储所有可能的“葫芦”组合
        List<int[]> possibleFullHouses = new ArrayList<>();

        // 遍历所有可能的三张相同牌面值的组合
        for (int key : countMap.keySet()) {
            if (countMap.get(key) >= 3) {
                // 找到所有可能的两张相同牌面值的组合
                for (int otherKey : countMap.keySet()) {
                    if (otherKey != key && countMap.get(otherKey) >= 2) {
                        // 计算“葫芦”组合的牌面值之和
                        int sum = 3 * key + 2 * otherKey;
                        if (sum <= max) {
                            possibleFullHouses.add(new int[]{key, otherKey});
                        }
                    }
                }
            }
        }

        // 如果没有符合条件的“葫芦”组合，返回 [0, 0]
        if (possibleFullHouses.isEmpty()) {
            return new int[]{0, 0};
        }

        // 选择符合条件的最大“葫芦”组合
        // 这里需要实现一个比较函数来比较“葫芦”组合的大小
        // 你可以使用 Collections.sort 或者手动比较
        // 返回最大的“葫芦”组合
        // 这里隐藏关键步骤，你可以根据需要实现
        return findMaxInCandidate(possibleFullHouses, max); // 这里需要返回实际的最大“葫芦”组合
    }

    private static int[] findMaxInCandidate(List<int[]> possibleFullHouses, int max) {
        int[] maxFullHouse = new int[]{0, 0};
        for (int[] fullHouse : possibleFullHouses) {
            if (fullHouse[0] !=1 && maxFullHouse  [0]!=1 ) {
                if(fullHouse[0] > maxFullHouse[0]) {
                    maxFullHouse = fullHouse;
                } else if (fullHouse[0] == maxFullHouse[0] ) {
                    if(fullHouse[1]==1){
                        maxFullHouse = fullHouse;
                    }else if(maxFullHouse[1] !=1 && fullHouse[1] > maxFullHouse[1]) {
                        maxFullHouse = fullHouse;
                    }
                }
            } else if((fullHouse[0] ==1  )) {
                if(maxFullHouse[0] != 1){
                    maxFullHouse = fullHouse;
                }else if( fullHouse[1] > maxFullHouse[1]){
                    maxFullHouse = fullHouse;
                }
            }
        }
        return maxFullHouse;
    }


    public static void main(String[] args) {
        // Add your test cases here

        System.out.println(java.util.Arrays.equals(solution(9, 42, new int[]{3,3,11,12,12,2,13,5,13,1,13,8,8,1,8,13,12,9,2,11,3,5,8,11,1,11,1,5,4,2,5}), new int[]{1, 13}));
        System.out.println(java.util.Arrays.equals(solution(9, 34, new int[]{6, 6, 6, 8, 8, 8, 5, 5, 1}), new int[]{8, 5}));
        System.out.println(java.util.Arrays.equals(solution(9, 37, new int[]{9, 9, 9, 9, 6, 6, 6, 6, 13}), new int[]{6, 9}));
        System.out.println(java.util.Arrays.equals(solution(9, 40, new int[]{1, 11, 13, 12, 7, 8, 11, 5, 6}), new int[]{0, 0}));
    }
}
